package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.mapper.CollectMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.CollectService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    /**
     * 收藏攻略的方法
     *
     *
     */
    @Override
    public void save(Collect collect) {
        //如果Strategy不为空,说明是攻略的收藏操作
        if (collect.getStrategy() != null ) {
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByStrategyIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException("你已经收藏过了该攻略");
            }
            collect.setCreateTime(new Date());
            collectMapper.insertStrategy(collect);
        }
        //如果Travel 不为空,说明是游记的收藏操作
        if (collect.getTravel() != null){
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByTravelIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException("你已经收藏过了该游记");
            }
            collect.setCreateTime(new Date());
            collectMapper.insertTravel(collect);
        }

        if (collect.getDailyNews() != null){
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByDailyNewsIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException("你已经收藏过了该日报");
            }
            collect.setCreateTime(new Date());
            collectMapper.insertDailyNews(collect);
        }
    }

    /**
     * 判断该用户是否收藏过该游记什么的
     * @param collect
     */
    @Override
    public void get(Collect collect) {
        if (collect.getStrategy() != null ) {
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByStrategyIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException();
            }
        }
        if (collect.getTravel() != null){
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByTravelIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException();
            }
        }
        if (collect.getDailyNews() != null){
            //通过用户id和攻略id查询,该用户是否收藏了该攻略,如果收藏后,就不能再次收藏相同的攻略
            Collect c = collectMapper.selectByDailyNewsIdAndUserId(collect);
            if (c != null) {
                throw new RuntimeException();
            }
        }

    }

    /**
     * 通过用户id和收藏攻略id或者游记id或者日报id,来删除收藏
     * @param collect
     */
    @Override
    public void delete(Collect collect) {
        if (collect.getStrategy() != null ) {
            collectMapper.deleteByUserIdAndgetStrategyId(collect);
        }
        if (collect.getTravel() != null){
            collectMapper.deleteByUserIdAndTravelId(collect);
        }
        if (collect.getDailyNews() != null){
            collectMapper.deleteByUserIdAndDailyNewsId(collect);
        }
    }

    @Override
    public PageInfo queryStrategy(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Collect> list = collectMapper.selectForListByStrategy(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryTravel(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Collect> list = collectMapper.selectForListByTravel(qo);
        return new PageInfo(list);
    }

    @Override
    public PageInfo queryDailyNews(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Collect> list = collectMapper.selectForListByDailyNews(qo);
        return new PageInfo(list);
    }


}
